<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="./css/base.css">
    <script src="./js/common.js"></script>
    <script src="./js/jquery-1.8.0.min.js"></script>
    <script src="./js/axios.js"></script>
    <script src="./js/template-web.js"></script>
    <title>电影首页</title>
    <script>
        window.onload = function () {
            // 搜索框功能
            let userInput = document.querySelector("#userInput");
            let seachBtn = document.querySelector("#seachBtn");
            userInput.onfocus = function () {
                seachBtn.style.display = "block";
            }
            userInput.onblur = function () {
                seachBtn.style.display = "none";
                userInput.value = "";
            }
        }

    </script>
</head>

<body>
    <!-- 项目容器 start-->
    <div class="app">
        <!-- 头部搜索框 start -->
        <nav class="nav-box">
            <div class="nav-input">
                    <input id="userInput" type="text" name='seach'>
            </div>
            <div id="seachBtn" class="nav-btn" onmousemove="doSearch()">
                <span >搜索</span>
            </div>
        </nav>
        <!-- 头部搜索框 end -->
        <!-- 首页正文 start -->
        <div class="content-box">
            <div class="home-box" id="box" >
                <!-- 首页分类列表 start -->
                <div>
                    <div class="list-name">
                        <span>热门电影</span>
                        <span class="list-more">更多 > </span>
                    </div>
                    <div class="list-content">
                        <!-- 电影展示组件 start -->
                        <div class="film-item">
                            <div class="film-img" style="background-image: url(./imgs/film.jpg)"></div>
                            <div class="film-title">蚁人2：黄蜂女现身</div>
                            <div class="film-source">
                                <span class="star-img open"></span>
                                <span class="star-img open"></span>
                                <span class="star-img open"></span>
                                <span class="star-img close"></span>
                                <span class="star-img close"></span>
                                <span class="star-source">7.9</span>
                            </div>
                        </div>
                        <!-- 电影展示组件 end -->
                        <!-- 电影展示组件 start -->
                        <div class="film-item">
                            <div class="film-img" style="background-image: url(./imgs/film.jpg)"></div>
                            <div class="film-title">蚁人2：黄蜂女现身</div>
                            <div class="film-source">
                                <!-- <span class="star-img open"></span>
                            <span class="star-img open"></span>
                            <span class="star-img open"></span>
                            <span class="star-img close"></span>
                            <span class="star-img close"></span>
                            <span class="star-source">7.9</span> -->
                                <span class="star-tip">暂无评分</span>
                            </div>
                        </div>
                        <!-- 电影展示组件 end -->
                        <!-- 电影展示组件 start -->
                        <div class="film-item">
                            <div class="film-img" style="background-image: url(./imgs/film.jpg)"></div>
                            <div class="film-title">蚁人2：黄蜂女现身</div>
                            <div class="film-source">
                                <span class="star-img open"></span>
                                <span class="star-img open"></span>
                                <span class="star-img open"></span>
                                <span class="star-img close"></span>
                                <span class="star-img close"></span>
                                <span class="star-source">7.9</span>
                            </div>
                        </div>
                        <!-- 电影展示组件 end -->
                        <!-- 电影展示组件 start -->
                        <div class="film-item">
                            <div class="film-img" style="background-image: url(./imgs/film.jpg)"></div>
                            <div class="film-title">蚁人2：黄蜂女现身</div>
                            <div class="film-source">
                                <span class="star-img open"></span>
                                <span class="star-img open"></span>
                                <span class="star-img open"></span>
                                <span class="star-img close"></span>
                                <span class="star-img close"></span>
                                <span class="star-source">7.9</span>
                            </div>
                        </div>
                        <!-- 电影展示组件 end -->
                        <!-- 电影展示组件 start -->
                        <div class="film-item">
                            <div class="film-img" style="background-image: url(./imgs/film.jpg)"></div>
                            <div class="film-title">蚁人2：黄蜂女现身</div>
                            <div class="film-source">
                                <span class="star-img open"></span>
                                <span class="star-img open"></span>
                                <span class="star-img open"></span>
                                <span class="star-img close"></span>
                                <span class="star-img close"></span>
                                <span class="star-source">7.9</span>
                            </div>
                        </div>
                        <!-- 电影展示组件 end -->
                        <!-- 电影展示组件 start -->
                        <div class="film-item">
                            <div class="film-img" style="background-image: url(./imgs/film.jpg)"></div>
                            <div class="film-title">蚁人2：黄蜂女现身</div>
                            <div class="film-source">
                                <span class="star-img open"></span>
                                <span class="star-img open"></span>
                                <span class="star-img open"></span>
                                <span class="star-img close"></span>
                                <span class="star-img close"></span>
                                <span class="star-source">7.9</span>
                            </div>
                        </div>
                        <!-- 电影展示组件 end -->
                    </div>
                </div>
                <script id="template" type="text/html">
                    {{each type t  i}}
                    <div>
                        <div class="list-name">
                            <span>{{t.typeName}}</span>
                            <span class="list-more">更多 > </span>
                        </div>
                        <div class="list-content">
                            {{each film[i] f}}
                            <!-- 电影展示组件 start -->
                            <div class="film-item">
                                <div class="film-img" style="background-image: url({{f.filmImg}})"></div>
                                <div class="film-title">{{f.filmName}}</div>
                                <div class="film-source">
                                    {{each grade g}}
                                    {{if f.source*1>=g}}
                                    <span class="star-img open"></span>
                                    {{else}}
                                    <span class="star-img close"></span>
                                    {{/if}}
                                    {{/each}}
                                    <span class="star-source">{{(f.source*1).toFixed(1)}}</span>
                                </div>
                            </div>
                            {{/each}}
                            <!-- 电影展示组件 end -->
                        </div>
                    </div>
                    {{/each}}
                </script>
                <script id="templates" type="text/html">
                    {{each type t  i}}
                    <div>
                        <div class="list-name">
                            <span>{{t.typeName}}</span>
                            <span class="list-more">更多 > </span>
                        </div>
                        <div class="list-content">
                            {{each film[i] f}}
                            <!-- 电影展示组件 start -->
                            <div class="film-item">
                                <div class="film-img" style="background-image: url({{f.filmImg}})"></div>
                                <div class="film-title">{{f.filmName}}</div>
                                <div class="film-source">
                                    {{each grade g}}
                                    {{if f.source*1>=g}}
                                    <span class="star-img open"></span>
                                    {{else}}
                                    <span class="star-img close"></span>
                                    {{/if}}
                                    {{/each}}
                                    <span class="star-source">{{(f.source*1).toFixed(1)}}</span>
                                </div>
                            </div>
                            {{/each}}
                            <!-- 电影展示组件 end -->
                        </div>
                    </div>
                    {{/each}}
                </script>
                <!-- 首页分类列表 end -->
            </div>
        </div>
        <!-- 首页正文 end -->
    </div>
    <!-- 项目容器 end-->
</body>
<script>
    async function loadType(){
        try {
            var {data:{resultState,result,msg}}=await axios.get('http://localhost/filmApi/loadTyps.php'); 
        } catch (error) {
            console.log(error);
            alert("数据加载失败，请重新加载");
        }finally{
            return result;
        }
    }
    async function loadFilms(){
        try {
            var {data:{resultState,result:data,msg}}=await axios.get('http://localhost/filmApi/loadTyps.php');
            var resultArr=[];
            for(let i=0;i<data.length;i++){
                var param=data[i]['typeId'];
                var {data:{resultState,msg,result:result}}= await axios.get('http://localhost/filmApi/loadFilms.php?typeId='+param);
                resultArr[i]=result;
            }
            return resultArr;

        } catch (error) {
            console.log(error);
        }
    }
    function allLoad(){
        let p1=this.loadType();
        let p2=this.loadFilms();
        Promise.all([p1,p2]).then((dataArray)=>{
            let [type,film]=dataArray;
            let html=template('template',{film,grade:[1,2,3,4,5],type})
            document.querySelector('#box').innerHTML = html
        })
    }
    allLoad();
    async function doSearch(){
            var param=$("#userInput").val();
            var {data:{resultState,msg,result:film}}= await axios.get('http://localhost/filmApi/loadFilms.php?seach='+param);
            let content=template('templates')
            document.querySelector('#box').innerHTML = content;
        }
</script>
</html>